package com.s1nx.back.controller;

import com.s1nx.back.dto.BookDTO;
import com.s1nx.back.dto.convert.BookConverter;
import com.s1nx.back.entity.Book;
import com.s1nx.back.service.BookService;
import com.s1nx.back.utils.Pagination;
import com.s1nx.back.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Result list(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                       @RequestParam(defaultValue = "10", value = "pageSize") Integer pageSize) {

        List<Book> allBooks = bookService.getAllBooks();
        List<BookDTO> bookDTOS = BookConverter.convertBookListToBookDTOList(allBooks);

        //设置页码范围
        if (pageNum < 1) {
            pageNum = 1;
        }
        if (pageNum > Pagination.getPages(bookDTOS.size(), pageSize)) {
            pageNum = Pagination.getPages(bookDTOS.size(), pageSize);
        }

        List<BookDTO> paginatedBookDTOS = Pagination.paginateList(bookDTOS, pageNum, pageSize);
        String total = String.valueOf(bookDTOS.size());

        return Result.withCode(200).withMsg("遍历成功").put("books", paginatedBookDTOS).put("total", total);
    }

    //删除书籍
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public Result delete(@RequestParam("id") Integer id) {

        int i = bookService.deleteBook(id);
        if (i == 0) {
            return Result.withCode(404).withMsg("删除失败，书籍不存在");
        }
        return Result.withCode(200).withMsg("删除成功");
    }

    //更新书籍信息
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ResponseBody
    public Result update(@RequestParam("id") Integer id,
                         @RequestParam("title") String title,
                         @RequestParam("author") String author,
                         @RequestParam("price") double price,
                         @RequestParam("num") int num) {
        Book book = new Book();
        book.setId(id);
        book.setTitle(title);
        book.setAuthor(author);
        book.setPrice(price);
        book.setNum(num);
        int i = bookService.updateBook(book);
        if (i == 0) {
            return Result.withCode(404).withMsg("更新失败，书籍不存在");
        }
        return Result.withCode(200).withMsg("更新成功");
    }

    //添加书籍
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result add(@RequestParam("title") String title,
                      @RequestParam("author") String author,
                      @RequestParam("price") double price,
                      @RequestParam("num") int num) {
        Book book = new Book();
        book.setTitle(title);
        book.setAuthor(author);
        book.setPrice(price);
        book.setNum(num);
        bookService.addBook(book);
        return Result.withCode(200).withMsg("添加成功");
    }

}
